﻿import { combineReducers } from 'redux'
import { UPD_AUTH, UPD_HIST, UPD_STAT, UPD_SEND } from './loader/actionTypes'
import send from './send/reducer'
import authorize from './authorize/reducer'
import statistic from './statistic/reducer'
import history from './history/reducer'
import updateStatisticState from './statistic/updateRootState'
import updateHistoryState from './history/updateRootState'


const combinedReducer = combineReducers({
    send,
    authorize,
    statistic,
    history
});

export default function (state, action) {
    switch (action.type) {
        case UPD_SEND:
            return updateDeps(state, 'send', action.data);
        case UPD_AUTH:
            return updateDeps(state, 'authorize', action.data);
        case UPD_HIST:
            return updateHistoryState(state, action.data);
        case UPD_STAT:
            return updateStatisticState(state, action.data);
        default:
            return combinedReducer(state, action)
    }
}
function updateDeps(state, containerName, departments) {
    var college = { expanded: true, departments };
    var container = state[containerName];
    var newContainer = container ? { ...container, college } : { college };
    return { ...state, [containerName]: newContainer }
}

